package stack;

public class Stack<T> {
    private Node first;//栈顶（最近添加的元素）
    private int N; //元素数量
    
    public boolean isEmpty(){
     return first ==null;   
    }
    
    public int size() {
        return N;
    }
    
    public void push(T t) {
        //栈顶添加元素
        Node oldFirst = first;
        first = new Node();
        first.t = t;
        first.next = oldFirst;
        N++;
    }
    
    public T pop() {
        //栈顶删除元素
        Node popNode = first;
        first = first.next;
        N--;
        return popNode.t;
    }
    
    private class Node{
        //定义节点的嵌套类
        T t;
        Node next;
    }
}
